// pages/personalCenter/index/index.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    marginTop: "",
    isFollow: false, //用户是否已经关注公众号
    isBind: false, //用户是否已经绑定手机号
    isLogin: false, //用户是否登录
    nickName:"",
    avatarUrl:"",
    post_collection:[],
    notice_collection:[],
    mobile:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(getApp().globalData.mobile != ""){
      this.setData({ mobile:getApp().globalData.mobile, isBind:true})
    }

    const statusBarHeight = wx.getWindowInfo().statusBarHeight;
    const menuInfo = wx.getMenuButtonBoundingClientRect();
    const marginTop =
      -(menuInfo.top - statusBarHeight + menuInfo.bottom) + "px";
    this.setData({ marginTop });
    let openid = getApp().globalData.openId;
    wx.$cloud.callFunction({
      name:"userModule",
      data:{
          action:"getUserInformation",
          openid:openid,
      },
      success:res=>{
        console.log(res);
        if(res.result!=null){
            this.setData({
                isLogin:true,
                nickName:res.result.nickname,
                avatarUrl:res.result.face,
                isLogin:true  
            })
        }
          let post_list=[]
          for(let i=0;i<res.result.post_collection.length;i++){
              const id = res.result.post_collection[i]
              wx.$cloud.callFunction({
                  name:"return-recruit-info",
                  data:{
                      action:"detail",
                      id:id
                  },
                  success:result=>{
                      const item = {...result.result.data}
                      console.log(item)
                      post_list.push(item)
                  },
                  fail:err=>{
                      console.error(err)
                  }
              })
          }
          this.setData({
              post_collection:post_list,
          })
          
      },
      fail:err=>{
        console.error(err);
      }
    })
  },
  /**
   * 跳转至岗位收藏
   */
  goPostCollection() {
    let post = JSON.stringify(this.data.post_collection);
    if(this.data.isLogin){
      wx.navigateTo({
        url:"/pages/personalCenter/collection/position/position?post=" + post
      });
    }
    else wx.$go2("/pages/personalCenter/collection/position/position?post="+post);
  },
  /**
   * 跳转至公告收藏
   */
  goNoticeCollection() {
    let notice = JSON.stringify(this.data.notice_collection);
    if(this.data.isLogin){
      wx.navigateTo({
        url:"/pages/personalCenter/collection/anouncement/anouncement?notice="+notice
      });
    }
    else wx.$go2("/pages/personalCenter/collection/anouncement/anouncement?notice="+notice);
  },
  connectWe(){
    // wx.showToast({
    //     title: '欢迎联系我们：15902055515',
    //     icon: 'none',
    //     image: '',
    //     duration: 1500,
    //     mask: false,
    //     success: (result)=>{
    //     },
    //     fail: ()=>{},
    //     complete: ()=>{}
    // });
    wx.makePhoneCall({
      phoneNumber: '15902055515',
      success:function(){
        wx.showToast({
          title: '拨打成功',
          icon: 'true',
          duration:2000
        })
      },
      fail:function(){
        wx.showToast({
          title: '拨打失败',
          icon:'error',
          duration:2000
        })
      }
    })
},

JumpToFeedback(){
  wx.navigateTo({
      url: '/pages/Feedback/Feedback',
      success: (result) => {

      },
      fail: () => {},
      complete: () => {}
  });
},

getPhoneNumber(e) {
  console.log(e);
  //store.SET_TOKEN('a132sdf54a6sdf2a1sd34');
  console.log(e)
  var that = this;
   wx.$cloud.callFunction({
     name:"getMobile",
     data: {
       action: "getMobile",
       weRunData: wx.cloud.CloudID(e.detail.cloudID),
     }
   }).then(
     res=>{
       let data = res.result.event.weRunData.data;
      if(data && data.phoneNumber){
        that.setData({
          mobile:res.result.event.weRunData.data.phoneNumber
        })
      }
      else {
        wx.showToast({title:"绑定失败",icon:"error"})
      }
       })
       .catch(
         err=>{
           console.error(err);
         }
       ) 
},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},
});
